@extends('page')
@section('body')
    <link rel="stylesheet" type="text/css" href="{{URL::asset('/assets/simditor/simditor.css')}}" />
    <script src="{{URL::asset('/assets/simditor/module.js')}}"></script>
    <script src="{{URL::asset('/assets/simditor/hotkeys.js')}}"></script>
    <script src="{{URL::asset('/assets/simditor/uploader.js')}}"></script>
    <script src="{{URL::asset('/assets/simditor/simditor.js')}}"></script>

    <link rel="stylesheet" type="text/css" href="{{URL::asset('/assets/select2/css/select2.css')}}" />
    <script src="{{URL::asset('/assets/select2/js/select2.js')}}"></script>
    <style>
        .layui-form-checkbox{
            margin-bottom: 5px;
        }
        img{
            width:auto;
            height:auto;
            max-width:100%;
            max-height:100%;
        }
    </style>
    <fieldset class="layui-elem-field">
        <?php $name = (new \App\Model\Page())->getTypeName($type)?>
        <legend>{{'添加'.$name}}</legend>
        <div class="layui-field-box">
            <div class="layui-row">
                <div class="layui-col-md8 left">
                    <form class="layui-form">
                        <div class="layui-form-item">
                            <div style="margin-left: 0px" class="layui-input-block">
                                <input id="title" name="title" type="text" placeholder="{{'输入'.$name.'标题'}}" class="layui-input">
                            </div>
                        </div>
                        @if($type == \App\Model\Page::TYPE_PAGE)
                        <div class="layui-form-item">
                            <label class="layui-form-label">固定链接:</label>
                            <div class="layui-input-block" style="line-height:36px">
                                <a id="url">{{url('index/index')}}</a><a class="layui-btn layui-btn-small">修改</a>
                            </div>
                        </div>
                        @endif
                        <div class="layui-form-item">
                            <div class="layui-input-block" style="margin-left: 0px">
                                <textarea id="editor" placeholder="Balabala" autofocus></textarea>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="layui-col-md4 right">
                    <div class="layui-collapse postbox">
                        <div class="layui-colla-item">
                            <h2 class="layui-colla-title">发布</h2>
                            <div class="layui-colla-content layui-show">
                                <ul>
                                    <li><i class="layui-icon">&#xe62e;</i>状态：发布</li>
                                    <input id="state" value='{{\App\Model\Page::STATE_PUBLISH}}' hidden>
                                    <li><i class="layui-icon">&#xe60f;</i>公开度：不公开</li>
                                    <input  id="secret" value='{{\App\Model\Page::SECRET_SINGLE}}' hidden>
                                </ul>
                                <div class="layui-btn-group">
                                    <div class="layui-btn layui-btn-small" onclick="">保存草稿</div>
                                    <div class="layui-btn layui-btn-small" onclick="ppost.savePost()" id="publish">发布</div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-collapse postbox">
                        <div class="layui-colla-item">
                            @if($type == \App\Model\Page::TYPE_PAGE)
                            <h2 class="layui-colla-title">页面属性</h2>
                            <div class="layui-colla-content layui-show">
                                <div class="layui-form">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">父级</label>
                                        <div class="layui-input-block">
                                            <select id="parent">
                                                <option value="1">(无父级)</option>
                                                @foreach($pages as $p)
                                                    <option value="{{$p->id}}">{{$p->title}}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">权重</label>
                                        <div class="layui-input-block">
                                            <input id="right" type="number" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                                @endif
                            @if($type == \App\Model\Page::TYPE_POST)
                                    <h2 class="layui-colla-title">分类目录</h2>
                                    <div class="layui-colla-content layui-show">
                                        <div class="layui-tab layui-tab-card">
                                            <ul class="layui-tab-title">
                                                <li class="layui-this">分类目录</li>
                                                <li>最常用</li>
                                            </ul>
                                            <div class="layui-tab-content">
                                                <div class="layui-tab-item layui-show">
                                                    <form class="layui-form">
                                                        @foreach($categorys as $category)
                                                            <input class="categorys" type="checkbox" id="" value="{{$category->id}}" name="categorys" title="{{$category->name}}">
                                                        @endforeach
                                                    </form>
                                                </div>
                                                <div class="layui-tab-item">2</div>
                                            </div>
                                        </div>
                                    </div>
                                @endif
                        </div>
                    </div>
                    @if($type == \App\Model\Page::TYPE_POST)
                    <div class="layui-collapse postbox">
                        <div class="layui-colla-item">
                                <h2 class="layui-colla-title">标签</h2>
                                <div class="layui-colla-content layui-show">
                                    <div class="layui-form">
                                        <div class="layui-form-item">
                                            <div class="layui-input-inline">
                                                <select id="tags" name="tags" style="width: 190px;height: 40px;" lay-ignore>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                        </div>
                    </div>
                    @endif
                    <div class="layui-collapse postbox">
                        <div class="layui-colla-item">
                            <h2 class="layui-colla-title">插入头图（默认图片）</h2>
                            <div class="layui-colla-content layui-show">
                                <div class="layui-form">
                                    <div class="layui-form-item">
                                        <img id="img" src="">
                                    </div>
                                    <div class="layui-form-item">
                                        <button type="button" class="layui-btn layui-btn-small" id="upload">
                                            <i class="layui-icon">&#xe67c;</i>{{'设置'.$name.'头图'}}
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </fieldset>
    <script>
        var categorys = [];
        layui.use(['form', 'layedit', 'laydate','upload'], function() {
            var form = layui.form;
            form.render('select');

            form.on('checkbox', function(data){
                if((index = $.inArray(data.value,categorys))>=0){
                    categorys.splice(index,1);
                }else{
                    categorys.push(data.value);
                }
                console.log(categorys);
            });

            var upload = layui.upload;
            var uploadInst = upload.render({
                elem: '#upload' //绑定元素
                ,url: '{{url("admin/upload")}}' //上传接口
                ,done: function(res){
                    //上传完毕回调
                    $('#img').attr('src',res.data.path);
                    window.parent.show_stack_success('图片上传成功');
                }
                ,error: function(){
                    window.parent.show_stack_error('图片上传失败');
                }
            });
        });
        var editor = new Simditor({
            textarea: $('#editor')
        });

        $("#tags").select2({
            placeholder: "请输入标签",
            minimumInputLength: 1,
            multiple: true,
            ajax: {
                url: "/admin/category/search",
                dataType: "json",
                type: "post",
                delay: 300,
                data: function (t) {
                    return {
                        'keyword': t.term,
                    };
                },
                processResults: function (data) {
                    console.log(data);
                    var ss=[];
                    $.each(data.data,function(){
                        var s={}
                        s.id = this.id;
                        s.text = this.name;
                        ss.push(s);
                    })
                    return {
                        results: ss
                    };
                },
            },
        });

        var ppost = {
            savePost : function () {
                var data = {};
                var type = '{{$type}}';
                switch (type){
                    case '{{\App\Model\Page::TYPE_PAGE}}':
                        data.url   = $("#url").text();
                        data.right = $("#right").val();
                        data.icon   = '';
                        data.parent = $("#parent").val();
                        break;
                    case '{{\App\Model\Page::TYPE_POST}}':
                        data.tags = $('#tags').val();
                        data.categorys = categorys;
                        break;
                    default:
                        window.parent.show_stack_error('参数错误');
                }
                data.img   = $('#img').attr('src');
                data.title = $("#title").val();
                data.state = $("#state").val();
                data.secret  = $("#secret").val();
                data.content = editor.getValue();
                data.type  = type;
                post('{{url("admin/page/add")}}',{data:data},function (res) {
                    if(res.ret == 1){
                        window.parent.show_stack_success(res.data);
                    }else{
                        window.parent.show_stack_error(res.msg);
                    }
                })
            }
        }
    </script>
    @endsection